/*
 * @Author: your name
 * @Date: 2024-04-04 22:15:50
 * @LastEditTime: 2024-04-04 22:31:16
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\350.两个数组的交集-ii.cpp
 */
/*
 * @lc app=leetcode.cn id=350 lang=cpp
 *
 * [350] 两个数组的交集 II
 */

// @lc code=start
class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        int len1 = nums1.size(),len2 = nums2.size();
        unordered_map<int , int > map;
        vector<int> res;
        if(len1 >= len2){
            // 统计较短的数组的哈希表
            for(int num:nums1){
                ++map[num];
            }
            for(int num:nums2){
                if(map.find(num) != map.end() && map[num] > 0){
                    --map[num];
                    res.emplace_back(num);
                }   
            }
        }
        else{
            return intersect(nums2,nums1);
        }
        return res;
        
    }
};
// @lc code=end

